package com.camel.docker.controller;

import com.camel.docker.utils.ExportExcelUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.charts.ChartDataSource;
import org.apache.poi.ss.usermodel.charts.DataSources;
import org.apache.poi.ss.usermodel.charts.PieChartData;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFChart;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/poi")
public class PoiController {

    @GetMapping
    public void d(HttpServletRequest req, HttpServletResponse resp) {
        ExportExcelUtils.export(fileExport(), "exportname", resp);
    }

    SXSSFWorkbook fileExport() {
        SXSSFWorkbook wb = new SXSSFWorkbook();
        Sheet sheet = wb.createSheet("Sheet 1");

        Row titleRow = sheet.createRow(0);
        titleRow.createCell(0).setCellValue("测试aSda岁的萨莎AW大大撒旦飞洒地方从AW达达娃大顶起定期存钱WEFWQEFGQ我的QWqwDADAWFXC qWAFSAFCWEEFA微风轻抚我的vgwegvwerbwefwqfwedf  权威的青蛙打网球的");

        Row row1 = sheet.createRow(1);
        row1.createCell(0).setCellValue("试试");
        row1.createCell(1).setCellValue(3);

        Row row2 = sheet.createRow(2);
        row2.createCell(0).setCellValue("的撒大");
        row2.createCell(1).setCellValue(4);

        Row row3= sheet.createRow(3);
        row3.createCell(0).setCellValue("是的气势非凡哇塞大大");
        row3.createCell(1).setCellValue(3);

        Drawing drawing = sheet.createDrawingPatriarch();
        ClientAnchor anchor = drawing.createAnchor(0, 0, 0, 0, 0, 5, 6, 15);
        anchor.setAnchorType(2);
        XSSFChart chart = (XSSFChart)drawing.createChart(anchor);
        PieChartData data = chart.getChartDataFactory().createPieChartData();
        //PieChartData data = new XSSFPieChartData();
        ChartDataSource<Number> xs = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 3, 0, 0));
        ChartDataSource<Number> ys1 = DataSources.fromNumericCellRange(sheet, new CellRangeAddress(1, 3, 1,1));

        data.addSerie(xs, ys1);
        chart.plot(data);
        return wb;
    }
}
